#include <stdio.h>
#include <stdlib.h>
#include "mathtable.h"


/* Private */
static 
int add(int x, int y){
  return (x+y);
}

/* Private */
static 
int sub(int x, int y){
  return (x-y);
}

/* Private */
static 
int slope(int x1, int y1, int x2, int y2){
  return ((y2-y1)/(x2-x1));
}

/* Private */
static
int mean (int *elements, int cnt){
  int sum=0;
  int *cur = elements;
  int i=0;
  for(i=0;i<cnt;i++){
    sum+= *cur;
    cur++;
  }
  return (sum/cnt);
}

int math_call(int function_name, void *args, int *result)
{
  if (function_name==ADD){
    struct args2* arguments = (struct args2*)args;
    *result = add(arguments->a1,arguments->a2);
    return 0;
  }else if (function_name==SUB){
    struct args2* arguments = (struct args2*)args;
    *result = sub(arguments->a1,arguments->a2);
    return 0;
  }else if (function_name==SLOPE){
    struct args4* arguments = (struct args4*)args;
    *result = slope(arguments->a1,arguments->a2,arguments->a3,arguments->a4);
    return 0;
  }else if (function_name==MEAN){
    struct args2_a1p* arguments = (struct args2_a1p*)args;
    *result = mean(arguments->a1, arguments->a2);
    return 0;
  }else{
    return -1;
  }
}


